<?php

/**
 * Zend_Controller_action, Zeigt Digramme in Kake, Linien und Balken Form
 * @author  Christian Greweling, Sascha Witte
 */
class AnalyticalController extends Zend_Controller_Action {

    public function init() {
        $this->view->headScript()->appendFile('/resources/jscript/jplot/jquery.jqplot.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.pieRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.donutRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.dateAxisRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.canvasTextRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.canvasAxisTickRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.categoryAxisRenderer.min.js');
        $this->view->headScript()->appendFile('/resources/jscript/jplot/plugins/jqplot.barRenderer.min.js');
        $this->view->headLink()->prependStylesheet('/resources/jscript/jplot/jquery.jqplot.css');

        $this->view->documentClasses = array('analytical');
        $this->_statusMessenger = $this->_helper->flashMessenger;
        $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
        $this->_errorMessenger->setNamespace('error');
        $this->view->statusMessages = $this->_statusMessenger->getMessages();
    }

    public function indexAction() {

        $this->drawAnalyticalWinnerCake();
        $this->drawAnalyticalLines();
        $this->drawAnalyticalGameCount();
    }

    /**
     * Linien-Diagramm
     * Liest Daten aus Datenbank und bereitet diese für JQPlot auf 
     */
    private function drawAnalyticalLines() {


        $db = Zend_Db_Table::getDefaultAdapter();
        $select = new Zend_Db_Select($db);
        $select->from('explicit_catan_points', array('explicit_catan_points.user_id', 'user.salutation', 'user.color', new Zend_Db_Expr('SUM( explicit_catan_points.points) as points')))
                ->joinInner('user', 'user.id = explicit_catan_points.user_id', array())
                ->joinInner('explicit_catan', 'explicit_catan.id = explicit_catan_points.game_id', array())
                ->joinInner('event', 'explicit_catan.event_id = event.id', array())
                ->columns('event.date')
                ->group('explicit_catan_points.user_id')
                ->group('event.date');

        $resultSet = $db->fetchAll($select);
        $linesUser = array();
        $linesColor = array();
        $hilfpoints = O;
        $user = NULL;
        $jsArray = array();
        $statsArray = array();

        foreach ($resultSet as $array) {

            if ($user == NULL) {
                $user = $array['user_id'];
                $linesUser[] = $array['salutation'];
                $linesColor[] = "#" . $array['color'];
            }
            //Die Tabelle ist nach usern Sotiert, soabald sich user ändert aufgebauten array hinzufügen und neuen beginnen
            if ($user != $array['user_id']) {
                $user = $array['user_id'];
                array_push($statsArray, $jsArray);
                $jsArray = Null;
                $hilfpoints = 0;
                echo $array['salutation'];
                $linesUser[] = $array['salutation'];
                $linesColor[] = "#" . $array['color'];
            }
            //Punkte aufaddieren 
            $hilfpoints += (int) $array['points'];
            $jsArray[] = array($array['date'], $hilfpoints);
        }
        array_push($statsArray, $jsArray);

        $this->view->lines = json_encode($statsArray);
        $this->view->lineColor = json_encode($linesColor);
        $this->view->lineUser = json_encode($linesUser);
/*


 

        $db = $this->getInvokeArg('bootstrap')->getResource('db');

        $sql = "SELECT explicit_catan_points.user_id, SUM( explicit_catan_points.points ) AS 'points', event.date
                FROM explicit_catan_points
                INNER JOIN (
                event
                INNER JOIN explicit_catan ON event.id = explicit_catan.event_id)
                ON explicit_catan.id = explicit_catan_points.game_id
                GROUP BY event.date, explicit_catan_points.user_id
                ORDER BY explicit_catan_points.user_id, event.date";
        $results = $db->fetchAll($sql);
        // var_dump($results);
        $hilfpoints = O;
        $user = NULL;
        $jsArray;
        $statssArray = array();

        $sql = "SELECT user.id, user.salutation, user.color from user";


        //durchläuft SQL-Ergebnis, baut einen Array auf nach User(Datum,Punkte an diesem Datum+Punkte vom vorigem DAtum)
        foreach ($results as $array) {

            if ($user == NULL) {
                $user = $array['user_id'];
            }
            //Die Tabelle ist nach usern Sotiert, soabald sich user ändert aufgebauten array hinzufügen und neuen beginnen
            if ($user != $array['user_id']) {
                $user = $array['user_id'];
                array_push($statssArray, $jsArray);
                $jsArray = Null;
                $hilfpoints = 0;
            }
            //Punkte aufaddieren 
            $hilfpoints += (int) $array['points'];
            $jsArray[] = array($array['date'], $hilfpoints);
        }
        // den letzen Array hinzufügen
        array_push($statssArray, $jsArray);

        $statsLines = json_encode($statsArray);

        //Color und User
        $sql = "SELECT user.id, user.salutation, user.color from user";
        $results = $db->fetchAll($sql);

        $salutation;
        $color;
        foreach ($results as $array) {
            $salutation[] = $array['salutation'];
            $color[] = "#" . $array['color'];
        }

        $salutation = json_encode($salutation);
        $color = json_encode($color);
        $this->view->salutation = $salutation;
        $this->view->color = $color;

        $this->view->statsLines = $statsLines;
      
        */
    }

    /**
     * KuchenDiagramm
     * Liest Daten aus Datenbank und bereitet diese für JQPlot auf 
     */
    public function drawAnalyticalWinnerCake() {
        $db = $this->getInvokeArg('bootstrap')->getResource('db');
        $sql = "SELECT user.`salutation`, user.`color`, COUNT(winner_id) as 'wins' FROM explicit_catan AS a LEFT JOIN user on  a.winner_id = user.id GROUP BY a.winner_id";
        $results = $db->fetchAll($sql);

        $jsArray;
        $color;
        //Daten nach Nutzername und Anzahl gewinne in Array schreiben, 
        //Farben der Nutzer in einen zweiten Array
        foreach ($results as $array) {
            $jsArray[] = array($array['salutation'], (int) $array['wins']);
            $color[] = "#" . $array['color'];
        }

        $statsCake = json_encode(array($jsArray));
        $color = json_encode($color);

        $this->view->color = $color;
        $this->view->statsCake = $statsCake;
    }

    /**
     * Balkendiagramm
     * Liest Daten aus Datenbank und bereitet diese für JQPlot auf 
     */
    private function drawAnalyticalGameCount() {

        
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = new Zend_Db_Select($db);
        $select->from('explicit_catan', array('explicit_catan_type.name', new Zend_Db_Expr('COUNT( explicit_catan.type_id) as plays')))
                ->joinLeft('explicit_catan_type', 'explicit_catan_type.id = explicit_catan.type_id', array())
                ->group('explicit_catan.type_id')
                ->order('plays DESC');

        $resultSet = $db->fetchAll($select);
        
        $jsArray = array();
        foreach ($resultSet as $array) {
            $jsArray[] = array((int) $array['plays'], $array['name']);
        }

        $statsGameCount = json_encode(array($jsArray));

        $this->view->statsGameCount = $statsGameCount;
    }

}

